<template>
  <el-main>

    <!-- 搜索框 -->
    <el-form ref="searchForm" :model="parm" label-width="80px" :inline="true" size="small">
      <el-form-item label="公告标题">
        <el-input v-model="parm.title" />
      </el-form-item>
      <el-form-item label="公告内容">
        <el-input v-model="parm.content" />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="searchBtn">查询</el-button>
        <el-button icon="el-icon-close" style="color:#ff7670" @click="resetBtn">重置</el-button>
        <el-button v-if="hasPerm('sys')" type="primary" icon="el-icon-plus" @click="addBtn">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableList" border stripe :height="tableHeight" size="small">
      <el-table-column label="公告标题" prop="title" />
      <el-table-column label="公告内容" prop="noticeContent" />
      <el-table-column label="创建时间" prop="createTime" />
      <el-table-column v-if="hasPerm('sys')" label="操作" align="center" width="280">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="editBtn(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteBtn(scope.row)">删除</el-button><!-- <el-button type="success" size="small"  v-if="scope.row.status == '0'" @click="doBtn(scope.row)">处理</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page.sync="parm.currentPage"
      :page-sizes="[10,20, 40, 80, 100]"
      :page-size="parm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="parm.total"
      background
      @size-change="sizeChange"
      @current-change="currentChange"
    />

    <!-- 弹框 -->
    <sys-dialog
      :title="addDialog.title"
      :width="addDialog.width"
      :height="addDialog.height"
      :visible="addDialog.visible"
      @onClose="onClose"
      @onConfirm="onConfirm"
    >
      <template slot="content">
        <el-form ref="addForm" :model="addModel" :rules="rules" label-width="80px" :inline="false" size="small">
          <el-form-item label="标题" prop="title">
            <el-input v-model="addModel.title" />
          </el-form-item>
          <el-form-item label="内容" prop="noticeContent">
            <el-input v-model="addModel.noticeContent" type="textarea" />
          </el-form-item>
        </el-form>
      </template>
    </sys-dialog>

  </el-main>
</template>

<script>
import { addApi, deleteApi, editApi, getListApi } from '@/api/notice'
import SysDialog from '@/components/system/SysDialog.vue'
import { getUserId } from '@/utils/auth'

export default {
  name: 'NoticeList',
  components: { SysDialog },
  data() {
    return {
      // 校验规则
      rules: {
        title: [{
          trigger: 'change',
          required: true,
          message: '请填写公告标题'
        }],
        noticeContent: [{
          trigger: 'change',
          required: true,
          message: '请填写公告内容'
        }]

      },
      parm: {
        total: 0, // 总条数
        pageSize: 10, // 页容量
        currentPage: 1, // 当前页
        title: '', // 公告标题
        noticeContent: ''// 公告内容
      },
      // 新增或编辑弹窗属性
      addModel: {
        complaintId: '',
        userId: '',
        editType: '', // 0:新增，1：编辑
        title: '',
        noticeContent: ''
      },
      // 弹框属性
      addDialog: {
        title: '',
        width: 600,
        height: 180,
        visible: false
      },
      tableList: [],
      tableHeight: 0
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 220
    })
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await getListApi(this.parm)
      if (res && res.code == 200) {
        this.tableList = res.data.records
        this.parm.total = res.data.total
      }
    },
    // 当前页改变时触发
    currentChange(val) {
      this.parm.currentPage = val
      this.getList()
    },
    // 页容量改变时触发
    sizeChange(val) {
      this.parm.pageSize = val
      this.getList()
    },
    // 新增按钮
    addBtn() {
      // 清空表单
      this.$resetForm('addForm', this.addModel)
      // 设置弹窗属性
      this.addDialog.title = '新增公告'
      this.addDialog.visible = true
      // 设置编辑状态
      this.addModel.editType = '0' // 0:新增，1：编辑
    },
    // 重置按钮
    resetBtn() {
      this.parm.noticeContent = ''
      this.parm.title = ''
      this.getList()
    },
    // 查询按钮
    searchBtn() {
      this.getList()
    },
    // 删除按钮
    async deleteBtn(row) {
      const confirm = await this.$myconfirm('确定要删除该数据吗?')
      if (confirm) {
        const res = await deleteApi({ noticeId: row.noticeId })
        if (res && res.code == 200) {
          // 提示信息,刷新页面
          await this.getList()
          this.$message.success(res.msg)
        }
      }
    },
    // 编辑按钮
    editBtn(row) {
      // 清空表单
      this.$resetForm('addForm', this.addModel)
      // 设置弹窗属性
      this.addDialog.title = '新增公告'
      this.addDialog.visible = true
      // 设置编辑状态
      this.addModel.editType = '0' // 0:新增，1：编辑
      // 将该行数据赋值到数据域
      this.$objCoppy(row, this.addModel)
    },
    // 弹窗确认
    onConfirm() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          let res = null
          this.addModel.userId = getUserId()
          if (this.addModel.editType == '0') { // 新增
            res = await addApi(this.addModel)
          } else {
            res = await editApi(this.addModel)
          }
          if (res && res.code == 200) {
            // 刷新页面，提示信息，关闭弹窗
            await this.getList()
            this.$message.success(res.msg)
            this.addDialog.visible = false
          }
        }
      })
    },
    // 弹窗关闭
    onClose() {
      this.addDialog.visible = false
    }
  }
}

</script>

<style scoped lang="scss">

</style>
